package com.example.demo.service.impl;

import com.example.demo.dao.CourseMaterialDao;
import com.example.demo.entity.CourseMaterialBean;
import com.example.demo.service.CourseMaterialService;
import com.example.demo.util.PageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseMaterialServiceImpl implements CourseMaterialService {

    @Autowired
    private CourseMaterialDao courseMaterialDao;

    @Override
    public List<CourseMaterialBean> all() {
        return courseMaterialDao.getAll();
    }

    @Override
    public Integer add(CourseMaterialBean courseMaterialBean) {
        return courseMaterialDao.insert(courseMaterialBean);
    }

    @Override
    public Integer update(CourseMaterialBean courseMaterialBean) {
        return courseMaterialDao.edit(courseMaterialBean);
    }

    @Override
    public Integer delete(Integer id) {
        return courseMaterialDao.del(id);
    }

    @Override
    public CourseMaterialBean one(Integer id) {
        return courseMaterialDao.getOne(id);
    }

    @Override
    public List<CourseMaterialBean> getByCourseId(Integer courseId) {
        return courseMaterialDao.getByCourseId(courseId);
    }

    @Override
    public PageEntity list(Integer pageNo, Integer pageSize, String name, Integer courseId, String sortOrder) {
        // 根据页码计算跳过的记录数
        Integer jump = (pageNo - 1) * pageSize;
        
        Integer count = courseMaterialDao.getCount(name, courseId);
        List<CourseMaterialBean> rows = courseMaterialDao.getPage(jump, pageSize, name, courseId, sortOrder);
        
        return new PageEntity<List>(rows, count);
    }
}
